function setdefalut() {
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var date = myDate.getDate();

    return year + "-" + month + "-" + date;
}

function loaddevice() {
    $("#device").empty();
    var category =$("#category").val();
    $.ajax({
        type: "post",
        url: "/equ/loadDevice",
        data:{category:category},
        dataType: "json",
        cache: false,
        async: false,
        success: function (data) {
            if (data != null) {
                $.each(data.dev, function (index, item) {
                    var op = new Option(item.devicec, item.devicecode);

                    document.getElementById("device").options.add(op)
                })

            }
        }
    })
}
function grantRoleFun(id) {
    var tt = ['goEqu', 'goHis', 'goIce', 'goGround','goHisPlay', 'goIcePlay', 'goGroundPlay', 'goAutoReport', 'goIceReport']
    layer.open({
        id: 'userEditId',
        type: 2,
        content: "/main/" + tt[id],

        maxmin: true,
        area: ['86%', '97%'],
        offset: ['0', '240px'],
        btn: ["取消"],

    })
}
function display(startTime,endTime) {
    if (typeof(startTime) == "undefined"){
        startTime = $("#startTime").val();
        endTime = $("#endTime").val();
    }
    var deviceCode = $("#device").val();
    loadWdEchart(startTime, endTime, deviceCode, "Hr_Temp_Avg", "平均气温", "温度", "°C");
    loadHREchart(startTime, endTime, deviceCode, "Hr_RH_Avg",   "平均湿度", "湿度", "%");
    loadWSForIndex(startTime, endTime, deviceCode,"2minws", "2min风速","风速","m/s");
    loadWDForIndex(startTime, endTime, deviceCode,"2minwd", "2min风向","风向","m/s");
    loadRainEchart(startTime, endTime, deviceCode,"Hr_Rain_Tot","降水量","降水量","mm");

}



function loadWdEchart(startTime, endTime, deviceCode, flag, title, tip, dw) {
    $.ajax({
        type: "post",
        url: "/history/loadWd",
        data: {
            "flag": flag,
            "startTime": startTime,
            "endTime": endTime,
            "deviceCode": deviceCode
        },
        success: function (data) {
            if (data.length > 0) {
                var obj = eval(data);

                var taskcharts = echarts.init(document.getElementById('temp'));
                taskcharts.clear();
                var option = {
                    title: {
                        text: title
                    },
                    tooltip: {
                        trigger: 'axis',
                        formatter: "时间:{b}<br/>" + tip + ":{c}" + dw
                    },
                    legend: {
                        data: ['最高']
                    },

                    toolbox: {
                        show: true,
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar']},
                            restore: {show: true},

                        }
                    },
                    calculable: true,
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: obj[0]
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            axisLabel: {
                                formatter: '{value} ' + dw
                            }
                        }
                    ],
                    series: [
                        {
                            name: tip,
                            type: 'line',
                            data: obj[1],
                            itemStyle: {
                                normal: {
                                    //好，这里就是重头戏了，定义一个list，然后根据所以取得不同的值，这样就实现了，
                                    color: '#be3ac0'
                                }
                            },
                            markPoint: {
                                data: [
                                    {type: 'max', name: '最大值'},
                                    {type: 'min', name: '最小值'}
                                ]
                            },
                            markLine: {
                                data: [
                                    {type: 'average', name: '平均值'}
                                ]
                            }
                        }
                    ]
                };
                taskcharts.setOption(option);
            }
        }
    });


}
function loadHREchart(startTime, endTime, deviceCode, flag, title, tip, dw) {
    $.ajax({
        type: "post",
        url: "/history/loadWd",
        data: {
            "flag": flag,
            "startTime": startTime,
            "endTime": endTime,
            "deviceCode": deviceCode
        },
        success: function (data) {
            if (data.length > 0) {
                var obj = eval(data);

                var taskcharts = echarts.init(document.getElementById('rh'));
                taskcharts.clear();
                var option = {
                    title: {
                        text: title
                    },
                    tooltip: {
                        trigger: 'axis',
                        formatter: "时间:{b}<br/>" + tip + ":{c}" + dw
                    },
                    legend: {
                        data: ['最高']
                    },

                    toolbox: {
                        show: true,
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar']},
                            restore: {show: true},

                        }
                    },
                    calculable: true,
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: obj[0]
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            axisLabel: {
                                formatter: '{value} ' + dw
                            }
                        }
                    ],
                    series: [
                        {
                            name: tip,
                            type: 'line',
                            itemStyle: {
                                normal: {
                                    //好，这里就是重头戏了，定义一个list，然后根据所以取得不同的值，这样就实现了，
                                    color: '#5646c0'
                                }
                            },
                            data: obj[1],
                            markPoint: {
                                data: [
                                    {type: 'max', name: '最大值'},
                                    {type: 'min', name: '最小值'}
                                ]
                            },
                            markLine: {
                                data: [
                                    {type: 'average', name: '平均值'}
                                ]
                            }
                        }
                    ]
                };
                taskcharts.setOption(option);
            }
        }
    });


}
function loadWSForIndex(startTime, endTime, deviceCode,flag,title,tip,dw) {
    $.ajax({
        type: "post",
        url: "/history/loadWSForIndex",
        data: {
            "flag": flag,
            "startTime":startTime,
            "endTime":endTime,
            "deviceCode":deviceCode
        },
        success: function (data) {
            if (data.length > 0) {
                var obj = eval(data);

                var taskcharts = echarts.init(document.getElementById('ws'));
                taskcharts.clear();
                var option = {
                    title: {
                        text: title
                    },
                    tooltip: {
                        trigger: 'axis',
                        //formatter: "时间:{b}点<br/>"+ tip+":{c}"+dw
                    },
                    legend: {
                        data: obj[3]
                    },
                    toolbox: {
                        show: true,
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    calculable: true,
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: obj[0]
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            axisLabel: {
                                formatter: '{value} '+dw
                            }
                        }
                    ],
                    series: [
                        {
                            name: obj[3][0],
                            type: 'line',
                            data: obj[1],
                            markPoint: {
                                data: [
                                    {type: 'max', name: '最大值'},
                                    {type: 'min', name: '最小值'}
                                ]
                            },
                            markLine: {
                                data: [
                                    {type: 'average', name: '平均值'}
                                ]
                            }
                        },{
                            name:  obj[3][1],
                            type: 'line',
                            data: obj[2],
                            markPoint: {
                                data: [
                                    {type: 'max', name: '最大值'},
                                    {type: 'min', name: '最小值'}
                                ]
                            },
                            markLine: {
                                data: [
                                    {type: 'average', name: '平均值'}
                                ]
                            }
                        }
                    ]
                };
                taskcharts.setOption(option);
            }
        }
    });



}
function loadWDForIndex(startTime, endTime, deviceCode,flag,title,tip,dw) {
    $.ajax({
        type: "post",
        url: "/history/loadWDForIndex",
        data: {
            "flag": flag,
            "startTime":startTime,
            "endTime":endTime,
            "deviceCode":deviceCode
        },
        success: function (data) {
            if (data.length > 0) {
                var obj = eval(data);

                var taskcharts = echarts.init(document.getElementById('wd'));

                var option = {
                    title : {
                        text: title
                    },
                    tooltip : {
                        trigger: 'axis',
                        formatter: function (params, ticket, callback) {
                            console.log(obj[2]);
                            var showHtm="风向次数";
                            for(var i=0;i<obj[2].length;i++){
                                if(i%2==0){
                                    showHtm+="<br>";
                                }
                                if((i-1)%2==0&&i!=0){
                                    showHtm+="&nbsp;&nbsp;&nbsp;&nbsp;";
                                }
                                var name =obj[2][i];
                                var value = obj[0][i];
                                showHtm+= name + '--' + value;

                            }
                            return showHtm;
                        }
                    },
                    legend: {
                        x : 'right',
                        y : 'bottom',
                        data:['风向次数']
                    },
                    toolbox: {
                        show : true,
                        feature : {
                            mark : {show: true},
                            dataView : {show: true, readOnly: false},
                            restore : {show: true},
                            saveAsImage : {show: true}
                        }
                    },
                    calculable : true,
                    polar : [
                        {
                            indicator : [
                                {text : 'N', max  : obj[1]},
                                {text : 'NNE', max  :  obj[1]},
                                {text : 'NE', max  :  obj[1]},
                                {text : 'ENE', max  :  obj[1]},
                                {text : 'E', max  :  obj[1]},
                                {text : 'ESE', max  :  obj[1]},
                                {text : 'SE', max  :  obj[1]},
                                {text : 'SSE', max  :  obj[1]},
                                {text : 'S', max  :  obj[1]},
                                {text : 'SSW', max  :  obj[1]},
                                {text : 'SW', max  :  obj[1]},
                                {text : 'WSW', max  :  obj[1]},
                                {text : 'W', max  :  obj[1]},
                                {text : 'WNW', max  :  obj[1]},
                                {text : 'NW', max  :  obj[1]},
                                {text : 'NNW', max  :  obj[1]}
                            ],
                            radius : 140,
                            center:['50%','55%'], // 图的位置
                        }
                    ],
                    series : [
                        {

                            type: 'radar',
                            tooltip: {
                                trigger: 'item'
                            },
                            itemStyle: {
                                /* normal: {areaStyle: {type: 'default'}}*/
                                normal: {
                                    color : "#1686c2", // 图表中各个图区域的边框线拐点颜色
                                    lineStyle: {
                                        color:"#1686c2" // 图表中各个图区域的边框线颜色
                                    },
                                    areaStyle: {
                                        type: 'default'
                                    }
                                }
                            },
                            data : [
                                {
                                    value :obj[0],
                                    name:'风向次数'

                                }
                            ]
                        }
                    ]
                };
                taskcharts.setOption(option);
            }
        }
    });



}
function loadRainEchart(startTime, endTime, deviceCode, flag, title, tip, dw) {
    $.ajax({
        type: "post",
        url: "/history/loadWd",
        data: {
            "flag": flag,
            "startTime": startTime,
            "endTime": endTime,
            "deviceCode": deviceCode
        },
        success: function (data) {
            if (data.length > 0) {
                var obj = eval(data);

                var taskcharts = echarts.init(document.getElementById('rain'));
                taskcharts.clear();
                var option = {
                    title: {
                        text: title
                    },
                    tooltip: {
                        trigger: 'axis',
                        formatter: "时间:{b}<br/>" + tip + ":{c}" + dw
                    },
                    legend: {
                        data: ['最高']
                    },

                    toolbox: {
                        show: true,
                        feature: {
                            mark: {show: true},
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['bar', 'line']},
                            restore: {show: true},

                        }
                    },
                    calculable: true,
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: false,
                            data: obj[0]
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            axisLabel: {
                                formatter: '{value} ' + dw
                            }
                        }
                    ],
                    series: [
                        {
                            name: tip,
                            type: 'bar',
                            itemStyle: {
                                normal: {
                                    //好，这里就是重头戏了，定义一个list，然后根据所以取得不同的值，这样就实现了，
                                    color: '#22c07c'
                                }
                            },
                            data: obj[1],
                            markPoint: {
                                data: [
                                    {type: 'max', name: '最大值'},
                                    {type: 'min', name: '最小值'}
                                ]
                            },
                            markLine: {
                                data: [
                                    {type: 'average', name: '平均值'}
                                ]
                            }
                        }
                    ]
                };
                taskcharts.setOption(option);
            }
        }
    });


}
var handleothers = function () {
   // $("#startTime").val(setdefalut());
    $("#startTime").val("2018-06-02");
}

var  handleDatepicker   =function () {
       $('.date').datepicker({
           todayHighlight: true,
           language: "zh-CN",
           autoclose: true,

       });
   }
   
var handleSearch =function () {
    $("#searchBtn").click(function () {
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        display(startTime,endTime);//主要是要这种写法

    });
    $("#tyear").click(function () {
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        display(startTime,endTime);//主要是要这种写法

    });
    $("#tquarter").click(function () {
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        display(startTime,endTime);//主要是要这种写法

    });
    $("#tmonth").click(function () {
        var startTime =getFirstDayOfWeek(new Date());
        var endTime = timeFormat(new Date());

        display(startTime,endTime);//主要是要这种写法

    });
    $("#tweek").click(function () {
        var startTime =getFirstDayOfWeek(new Date());
        var endTime = timeFormat(new Date());
        display(startTime,endTime);//主要是要这种写法

    });
    $("#tday").click(function () {
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        display(startTime,endTime);//主要是要这种写法

    });
}
var FormSearch = function () {
    "use strict";
    return {
        //main function
        init: function () {
            handleothers();
            handleDatepicker();

            handleSearch();

        }
    };
}();